package net2;

import java.io.*;
import java.net.Socket;
import java.nio.BufferOverflowException;
import java.util.Scanner;

public class TcpEchoClient {

    private static final int PORT = 4090;
    private static String hostDomain = "localhost";
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            Socket socket = new Socket(hostDomain, PORT);
            System.out.println("客户端：" +
                    socket.getLocalPort() +
                    " 已启动...");
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            System.out.print("请输入要传送的内容：");
            pw.println(scanner.next());
            pw.flush();
            System.out.println("内容已发送...");

            InputStream is = socket.getInputStream();
            BufferedReader bf = new BufferedReader(
                    new InputStreamReader(is, "UTF-8"));
            String line;
            System.out.println("服务器返回:");
            while ((line = bf.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println("-----------------");
            socket.close();
        }
    }

}
